import * as types from "../types/orderListTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import ApiConstants from "../common/ApiConstants";
import { toastShort } from "../utils/ToastUtils";
import EnumConstants from "../common/EnumConstants";

const { OrderType, OrderStatus } = EnumConstants;

export function getOrderList(type) {
  return dispatch => {
    dispatch({ type: types.ORDERLIST_INIT });
    let apiCode;
    if (type === OrderType.RECYCLE) {
      apiCode = ApiConstants.ORDERLIST_RECYCLE;
    } else if (type === OrderType.SERVICE) {
      apiCode = ApiConstants.ORDERLIST_SERVICE;
    } else {
      apiCode = ApiConstants.ORDERLIST_UPDATE;
    }
    NetWorkUtil.post(apiCode, {})
      .then(data => {
        dispatch({ type: types.ORDERLIST_DOWN, orderList: data.list });
      })
      .catch(error => {
        toastShort(error.message);
        dispatch({ type: types.ORDERLIST_ERROR, error });
      });
  };
}

export function unmount() {
  return dispatch => {
    dispatch({ type: types.ORDERLIST_UNMOUNT });
  };
}
